//
// Created by 徐套 on 2023/9/18.
//

#include "config.h"
#include "json.hpp"
#include <fstream>

using json = nlohmann::json;

std::string readFile(const std::string file_path) {
      std::ifstream ifs(file_path);
      if(!ifs){
          throw std::runtime_error("Failed to open file: " + file_path);
      }
      std::string content;
      content.assign((std::istreambuf_iterator<char>(ifs) ),(std::istreambuf_iterator<char>()));
      ifs.close();
      return content;
}

void config::load(std::string filename){
    std::string content = readFile(filename);
    json json = json::parse(content);
    serverConfig.port = json["server"]["port"];
    serverConfig.worker = json["server"]["workers"];

    loggerConfig.level = json["logger"]["level"];
    loggerConfig.output = json["logger"]["output"];
}

ServerConfig config::getServerConfig(){
    return serverConfig;
}

LoggerConfig config::getLoggerConfig() {
    return loggerConfig;
}


